home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / vbcc / machines / amigappc / libsrc / _main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  3.3 KB  |  137 lines

  1. /*
  2. ** _main() function for vbcc-Amiga-PowerPC
  3. **
  4. ** based on machines/m68k/libsrc/_main.c by Volker Barthelmann
  5. **
  6. **
  7. ** V0.3 18-Oct-97 phx
  8. **      Opens the dos.library and initializes DOSBase.
  9. **      IsInteractive() is available now. startup.s was improved
  10. **      to work with stdin/stdout redirection and to supply us
  11. **      with SysBase.
  12. ** V0.2 07-Oct-97 phx
  13. **      There is no IsInteractive() in the PowerUp-Kernel. Also, the
  14. **      current startup code only works with CONSOLE:.
  15. ** V0.1 04-Oct-97 phx
  16. **      adapted
  17. */
  18.  
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22.  
  23. #include <exec/types.h>
  24. #include <exec/execbase.h>
  25. #include <exec/libraries.h>
  26. #include <powerup/gcclib/powerup_protos.h>
  27.  
  28.  
  29. /* from startup.s */
  30. extern char *_stdin,*_stdout,*_stderr;
  31. extern struct ExecBase *SysBase;
  32.  
  33. FILE *stdin,*stdout,*stderr,*_firstfile=0,*_lastfile=0;
  34. struct __exitfuncs *__firstexit;
  35. struct Library *DOSBase = NULL;
  36.  
  37.  
  38. extern int main(int, char **);
  39. extern void _exit();
  40.  
  41.  
  42.  
  43. /* Three AmigaOS library calls are required in this startup code */
  44.  
  45. static LONG IsInteractive(BPTR file)
  46. {
  47.   struct Caos MyCaos;
  48.  
  49.   MyCaos.d1 = (ULONG)file;
  50.   MyCaos.M68kCacheMode = IF_CACHEFLUSHALL;
  51.   MyCaos.PPCCacheMode = IF_CACHEFLUSHALL;
  52.   MyCaos.caos_Un.Offset = -216;
  53.   MyCaos.a6 = (ULONG)DOSBase;
  54.   return((LONG)PPCCallOS(&MyCaos));
  55. }
  56.  
  57.  
  58. static struct Library *OpenLibrary(UBYTE *name,unsigned long ver)
  59. {
  60.   struct Caos MyCaos;
  61.  
  62.   MyCaos.a1 = (ULONG)name;
  63.   MyCaos.d0 = ver;
  64.   MyCaos.M68kCacheMode = IF_CACHEFLUSHALL;
  65.   MyCaos.PPCCacheMode = IF_CACHEFLUSHALL;
  66.   MyCaos.caos_Un.Offset = -552;
  67.   MyCaos.a6 = (ULONG)SysBase;
  68.   return((struct Library *)PPCCallOS(&MyCaos));
  69. }
  70.  
  71.  
  72. static void CloseLibrary(struct Library *libbase)
  73. {
  74.   struct Caos MyCaos;
  75.  
  76.   MyCaos.a1 = (ULONG)libbase;
  77.   MyCaos.M68kCacheMode = IF_CACHEFLUSHALL;
  78.   MyCaos.PPCCacheMode = IF_CACHEFLUSHALL;
  79.   MyCaos.caos_Un.Offset = -414;
  80.   MyCaos.a6 = (ULONG)SysBase;
  81.   PPCCallOS(&MyCaos);
  82. }
  83.  
  84.  
  85. void exit(int returncode)
  86. /* exit() function for the vbcc-Amiga-PowerPC version */
  87. {
  88.   struct __exitfuncs *p=__firstexit;
  89.  
  90.   while(p) {
  91.     p->func();  /* execute atexit() routines */
  92.     p=p->next;
  93.   }
  94.   while(_firstfile && !fclose(_firstfile));  /* close all open files */
  95.   _freemem();  /* free all memory */
  96.   if (DOSBase)
  97.     CloseLibrary(DOSBase);
  98.   _exit(returncode);
  99. }
  100.  
  101.  
  102. void _main(int argc, char **argv)
  103. {
  104.   if (!(DOSBase = OpenLibrary("dos.library",36)))
  105.     exit(EXIT_FAILURE);
  106.   stdin = (FILE *)malloc(sizeof(FILE));
  107.   stdout = (FILE *)malloc(sizeof(FILE));
  108.   stderr = (FILE *)malloc(sizeof(FILE));
  109.   if(!stdin || !stdout || !stderr)
  110.     exit(EXIT_FAILURE);
  111.   stdin->filehandle = _stdin;
  112.   stdin->flags = _READABLE;
  113.   if (IsInteractive((BPTR)_stdin))
  114.     stdin->flags |= _UNBUF;
  115.   stdout->filehandle = _stdout;
  116.   stdout->flags = _WRITEABLE;
  117.   if (IsInteractive((BPTR)_stdout))
  118.     stdout->flags |= _LINEBUF;
  119.   stderr->filehandle = _stderr;
  120.   stderr->flags = _WRITEABLE;
  121.   if (IsInteractive((BPTR)_stderr))
  122.     stderr->flags |= _UNBUF;
  123.   stdin->pointer = stdout->pointer = stderr->pointer=0;
  124.   stdin->base = stdout->base = stderr->base = 0;
  125.   stdin->count = stdout->count = stderr->count = 0;
  126.   stdin->bufsize = stdout->bufsize = stderr->bufsize = 0;
  127.   stdin->prev = 0;
  128.   stdin->next = stdout;
  129.   stdout->prev = stdin;
  130.   stdout->next = stderr;
  131.   stderr->prev = stdout;
  132.   stderr->next = 0;
  133.   _firstfile = stdin;
  134.   _lastfile = stderr;
  135.   exit(main(argc,argv));
  136. }
  137.